<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 李邦琦 <1536549251@qq.com>
// +----------------------------------------------------------------------

namespace app\crm\model;
use think\Db;
use think\Model;
use think\Cache;
use think\Loader;

class Taskcount extends Model
{
    /**
     * 获取任务列表
     * @param  string $where 获取条件
     * @return array         获取到的数据
     */
    public function taskCountData ($where)
    {
        $page = $_REQUEST["page"];
        $limit = $_REQUEST["limit"];
        $users = Cache::get('users');
        $task = Cache::get('task');
        $count = Db::name('vip_task')->where($where)->count();
        // 查询状态为1的用户数据 并且每页显示5条数据
        $taskData = Db::name('vip_task')->where($where)->limit(($page-1)*$limit,$limit)->order('createtime DESC')->select();
        foreach ($taskData as &$val) {
            $val['createuser'] = $users[$val['createuser']]['cn_name'];
            $val['username'] = $users[$val['username']]['cn_name'];
            $val['task_type'] = $task[$val['task_type']]['task_type'];
            // 判断任务是否截止 已截止显示任务详情 未截止显示当前任务进度
            if($val['endtime'] < date('Y-m-d H:i:s')) {
                // 计算任务进度
                $taskAll = Db::name('vip_customer_basic')->where('task_id='.$val['id'])->count();
                $taskComplete = Db::name('vip_visit_record')->field('count(*) as task_count')->where('task_id='.$val['id'])->group('task_id')->select();
                // print_r( $taskComplete);exit;
                $percent = ($taskComplete[0]['task_count']/$taskAll*100)."%";
                $val['task_detail'] = "任务已完成".$percent."，当前任务已到期";
            } else {
                // 计算任务进度
                $taskAll = Db::name('vip_customer_basic')->where('task_id='.$val['id'])->count();
                $taskComplete = Db::name('vip_visit_record')->field('count(*) as task_count')->where('task_id='.$val['id'])->group('task_id')->select();
                // print_r( $taskComplete);exit;
                $percent = ($taskComplete[0]['task_count']/$taskAll*100)."%";
                // echo $percent;exit;
                // $percent = "70%";
                $val['task_detail'] = '<div class="layui-progress layui-progress-big" lay-showPercent="yes" style="margin-top:5px;"><div class="layui-progress-bar  layui-bg-red" lay-percent="'.$percent.'"></div></div>';
            }
        }
        $rowsList = count($taskData) ? $taskData : false;
        $arrData["code"] = 0;
        $arrData["msg"] = '';
        $arrData["count"] = $count;
        $arrData["data"] = $rowsList;
        // print_r($arrData);exit;
        return $arrData;
    }


    public function taskDetailData ($id)
    {
        $taskData = Db::name('vip_task')->field('task_result')->where("id=$id")->find();
        return $taskData;
    }


}